<?php
/**
 * Created by PhpStorm.
 * User: edmmss
 * Date: 2018/11/14
 * Time: 14:45
 */

namespace app\modules\staticFactory;


use app\modules\staticFactory\format\AbstractFormatter;
use app\modules\staticFactory\format\FormatNumber;
use app\modules\staticFactory\format\FormatString;
use yii\base\InvalidArgumentException;

final class StaticFactory
{
    const TYPE_STRING = 'string';
    const TYPE_NUMBER = 'number';

    /**
     * 工厂实例
     *
     * @date   2018/11/14
     * @author edmmss
     * @param string $type
     * @return AbstractFormatter
     */
    public static function factory(string $type): AbstractFormatter
    {
        if ($type == self::TYPE_STRING) {
            return new FormatString();
        } elseif ($type == self::TYPE_NUMBER) {
            return new FormatNumber();
        } else {
            throw new InvalidArgumentException('Unknown format given');
        }
    }
}